﻿using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Core.Domain;
using QDT.MVC.Security;
using QDT.Web.Admin.Helper;
using QDT.Web.Admin.Models;
using QDT.Common.Data;
namespace QDT.Web.Admin.Controllers
{
    //[SecurityModule(Name = "友情链接分组管理")]
    public class LinkGroupController : BaseController
    {
        //
        // GET: /LinkGroup/
        //[SecurityNode(Name = "列表")]
        public ActionResult Index(GridSortOptions sort)
        {
            if (sort.Column == null) sort.Column = "ID";
            ViewData["sort"] = sort;
            var source = DbProvider.D<LinkGroup>().OrderBy(sort.Column, sort.Direction == SortDirection.Ascending).ToList();
            return View(source.ToModelList());
        }
        //[SecurityNode(Name = "编辑页面")]
        public ActionResult Edit(int id) {
            var data = DbProvider.GetByID<LinkGroup>(id);
            return View(data.ToModel());
        }
        //[SecurityNode(Name = "编辑")]
        public ActionResult PostEdit(LinkGroupModel model) {
            var data = DbProvider.GetByID<LinkGroup>(model.Id);
            data.Name = model.Name;
            data.Sort = model.Sort;
            data.IsEffect = model.IsEffect;
            DbProvider.Update(data);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }

        [HttpGet]
        //[SecurityNode(Name = "新增页面")]
        public ActionResult Add()
        {
            return View();
        }
        //[SecurityNode(Name = "新增")]
        public ActionResult PostAdd(LinkGroupModel model) {
            var entity = model.ToEntity();
            DbProvider.Add(entity);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }
        //[SecurityNode(Name = "彻底删除")]
        public JsonResult ForeverDel(int[] id) {
            try {
                foreach(var i in id) {
                    int tmpId = i;
                    DbProvider.Delete<LinkGroup>(o=>o.ID == tmpId);
                }
                DbProvider.SaveChanges();
                return Json(new {
                    Status = true, Info = "操作成功"
                });
            }catch(Exception ex) {
                return Json(new {
                    Status = false, Info = ex.Message
                });
            }
        }

    }
}
